#include "SurfaceSource.h"

#include "ToolBar.h"

#include <QColorDialog>

SurfaceSource::SurfaceSource(QString text) : Source(text)
{
	m_type = stSurface;
	m_vtkPolyData=NULL;
	m_opacity=100;
	m_surfaceColor = QColor(255,255,255);

	m_opacity = 1.0;
	m_smSurfaceValue = 1;
	m_smContour = NULL;
	m_smPolyDataMapper = NULL;
	m_smActor = NULL;;
}


SurfaceSource::~SurfaceSource()
{

}

// void SurfaceSource::setData( vtkImageData* data)
// {
// 	m_vtkImage = data;
// }

void SurfaceSource::setData(vtkPolyData *data, vtkImageData *idata)
{
	m_vtkPolyData = data;
	m_vtkImage = idata;
}

// vtkImageData* SurfaceSource::data()
// {
// 	return m_vtkImage;
// }

vtkPolyData* SurfaceSource::sdata()
{
	return m_vtkPolyData;
}

void SurfaceSource::setOpacity( float opacity )
{
	m_opacity = opacity;

	if ( m_smActor)
	{
		m_smActor->GetProperty()->SetOpacity(m_opacity);
		emitPropertyChanged(spSurfaceOpacity);
	}
}

float SurfaceSource::opacity()
{
	return m_opacity;
}



void SurfaceSource::setSurfaceColor(int colorTableItem)
{
	if( colorTableItem > g_colorTable->count()-1 || colorTableItem < 0)
		return;

	setSurfaceColor((*g_colorTable)[colorTableItem]);
}


void SurfaceSource::setSurfaceColor(const QColor &color) 
{ 
	m_surfaceColor = color;

	if ( m_smActor)
		m_smActor->GetProperty()->SetColor((double)m_surfaceColor.red()/255.0,
		(double)m_surfaceColor.green()/255.0, (double)m_surfaceColor.blue()/255.0);
}


QColor SurfaceSource::surfaceColor() 
{ 
	return m_surfaceColor;
}



void SurfaceSource::rebuildSurface(int n, double start, double end)
{
	if ( m_smContour)
	{
		m_smContour->GenerateValues(n, start, end);

		sourceModel(true);

		emitPropertyChanged(spRebuildSurface);
	}
}

vtkProp* SurfaceSource::sourceModel( bool refresh/*=false*/ )
{
	if ( m_smActor && !refresh)
		return m_smActor;

	if ( m_vtkImage)
	{
		double range[2];
		m_vtkImage->GetScalarRange(range);


		if ( !m_smContour)
			m_smContour = vtkContourFilter::New();
		m_smContour->SetInput(m_vtkImage);
		m_smContour->SetValue(0,(range[0]+range[1])/2.0);
		m_smContour->Update();

		if ( !m_smPolyDataMapper)
			m_smPolyDataMapper = vtkPolyDataMapper::New();

		m_smPolyDataMapper->SetInput(m_smContour->GetOutput());
		m_smPolyDataMapper->ScalarVisibilityOff();

		if ( !m_smActor)
			m_smActor = vtkActor::New();

		m_smActor->SetMapper(m_smPolyDataMapper);
		//m_smActor->PickableOn();


		if ( g_toolBar && g_toolBar->getColorDialogStatus() == true)
		{
			QColor color = QColorDialog::getColor();
			if ( color.isValid())
				m_smActor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
		}

		m_vtkPolyData = m_smContour->GetOutput();
	}
	else if ( m_vtkPolyData)
	{
		if ( !m_smPolyDataMapper)
			m_smPolyDataMapper = vtkPolyDataMapper::New();
		
		m_smPolyDataMapper->SetInput(m_vtkPolyData);
		m_smPolyDataMapper->ScalarVisibilityOff();

		if ( !m_smActor)
			m_smActor = vtkActor::New();
		
		m_smActor->SetMapper(m_smPolyDataMapper);
	}

	return m_smActor;
}

vtkActor* SurfaceSource::outlineActor()
{
	if ( !m_vtkPolyData)
		return NULL;

	if ( m_outlineActor)
		return m_outlineActor;

	m_outlineFilter = vtkOutlineFilter::New();
	m_outlineFilter->SetInput(m_vtkPolyData);

	m_outlinePolyDataMapper = vtkPolyDataMapper::New();
	m_outlinePolyDataMapper->SetInputConnection(m_outlineFilter->GetOutputPort());

	m_outlineActor = vtkActor::New();
	m_outlineActor->SetMapper(m_outlinePolyDataMapper);

	return m_outlineActor;
}

double* SurfaceSource::getPosition()
{
	if ( m_smActor)
	{
		return m_smActor->GetPosition();
	}
	else
		return NULL;
}

double* SurfaceSource::getBounds()
{
	if ( m_smActor)
	{
		return m_smActor->GetBounds();
	}
	else
		return NULL;
}

void SurfaceSource::setPosition( double* pos)
{
	if ( m_smActor)
	{
		m_smActor->SetPosition(pos);
		emitPropertyChanged(spPositionChanged);
	}
}




